package com.smart.translator.ui.voice;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.smart.translator.R;
import com.smart.translator.service.VoskSpeechService;

/**
 * Vosk语音识别测试Activity
 */
public class VoskTestActivity extends AppCompatActivity {
    private static final String TAG = "VoskTestActivity";
    private static final int REQUEST_RECORD_AUDIO_PERMISSION = 1001;
    
    private TextView tvStatus;
    private Button btnStartService;
    private Button btnStopService;
    private Button btnBack;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vosk_test);
        
        initViews();
        setupListeners();
        checkPermissions();
    }
    
    private void initViews() {
        tvStatus = findViewById(R.id.tv_status);
        btnStartService = findViewById(R.id.btn_start_service);
        btnStopService = findViewById(R.id.btn_stop_service);
        btnBack = findViewById(R.id.btn_back);
        
        updateStatus("准备就绪");
    }
    
    private void setupListeners() {
        btnBack.setOnClickListener(v -> finish());
        
        btnStartService.setOnClickListener(v -> {
            if (checkRecordAudioPermission()) {
                startVoskService();
            } else {
                requestRecordAudioPermission();
            }
        });
        
        btnStopService.setOnClickListener(v -> {
            stopVoskService();
        });
    }
    
    private void startVoskService() {
        try {
            Intent intent = new Intent(this, VoskSpeechService.class);
            startService(intent);
            updateStatus("Vosk服务已启动");
            Toast.makeText(this, "Vosk语音识别服务已启动", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Log.e(TAG, "启动Vosk服务失败", e);
            updateStatus("启动服务失败: " + e.getMessage());
            Toast.makeText(this, "启动服务失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
    
    private void stopVoskService() {
        try {
            Intent intent = new Intent(this, VoskSpeechService.class);
            stopService(intent);
            updateStatus("Vosk服务已停止");
            Toast.makeText(this, "Vosk语音识别服务已停止", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Log.e(TAG, "停止Vosk服务失败", e);
            updateStatus("停止服务失败: " + e.getMessage());
            Toast.makeText(this, "停止服务失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
    
    private void updateStatus(String status) {
        tvStatus.setText("状态: " + status);
        Log.d(TAG, "状态更新: " + status);
    }
    
    private boolean checkRecordAudioPermission() {
        return ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) 
               == PackageManager.PERMISSION_GRANTED;
    }
    
    private void requestRecordAudioPermission() {
        ActivityCompat.requestPermissions(this, 
            new String[]{Manifest.permission.RECORD_AUDIO}, 
            REQUEST_RECORD_AUDIO_PERMISSION);
    }
    
    private void checkPermissions() {
        if (!checkRecordAudioPermission()) {
            requestRecordAudioPermission();
        }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 
                                         @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        
        if (requestCode == REQUEST_RECORD_AUDIO_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "录音权限已授予", Toast.LENGTH_SHORT).show();
                updateStatus("录音权限已授予");
            } else {
                Toast.makeText(this, "需要录音权限才能使用语音识别功能", Toast.LENGTH_SHORT).show();
                updateStatus("录音权限被拒绝");
            }
        }
    }
} 